@PRO_MAKEFILE_INC@
prefix = @prefix@
datadir = @datadir@
SHELL=/bin/sh
OS := $(shell uname -s)
PWD=@PWD@
GPP=@GPP@
INSTALL_DIR=$(prefix)
MAN_DIR=$(prefix)
######
HAS_NDPI=$(shell pkg-config --exists libndpi; echo $$?)
ifeq ($(HAS_NDPI), 0)
    NDPI_INC = $(shell pkg-config --cflags libndpi | sed -e 's/\(-I[^ \t]*\)/\1\/libndpi/g')
    NDPI_LIB = $(shell pkg-config --libs libndpi)
    NDPI_LIB_DEP =
else
    NDPI_INC=-I@NDPI_HOME@/src/include
    NDPI_LIB=@NDPI_HOME@/src/lib/.libs/libndpi.a
    NDPI_LIB_DEP=$(NDPI_LIB)
endif
######
LIBPCAP=-lpcap
######
MONGOOSE_HOME=${PWD}/third-party/mongoose
MONGOOSE_INC=-I$(MONGOOSE_HOME)
######
HAS_LUAJIT=$(shell pkg-config --exists luajit; echo $$?)
ifeq ($(HAS_LUAJIT), 0)
	LUAJIT_INC = $(shell pkg-config --cflags luajit)
	LUAJIT_LIB = $(shell pkg-config --libs luajit)
else
	LUAJIT_HOME=${PWD}/third-party/LuaJIT-2.0.3
	LUAJIT_INC=-I$(LUAJIT_HOME)/src
	LUAJIT_LIB=$(LUAJIT_HOME)/src/libluajit.a
endif
######
LIBRRDTOOL_HOME=${PWD}/third-party/rrdtool-1.4.8
HAS_LIBRRDTOOL=$(shell pkg-config --exists librrd; echo $$?)
ifeq ($(HAS_LIBRRDTOOL), 0)
	LIBRRDTOOL_INC = $(shell pkg-config --cflags librrd)
	LIBRRDTOOL_LIB = $(shell pkg-config --libs librrd)
else
	LIBRRDTOOL_INC=-I$(LIBRRDTOOL_HOME)/src/
	ifeq ($(OS), OpenBSD)
		LIBRRDTOOL_LIB=$(LIBRRDTOOL_HOME)/src/.libs/librrd_th.a -lm -lgobject-2.0 -lgmodule-2.0 -lglib-2.0	
	else
		LIBRRDTOOL_LIB=$(LIBRRDTOOL_HOME)/src/.libs/librrd_th.a -lm -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0
	endif
endif
######
HTTPCLIENT_INC=${PWD}/third-party/http-client-c/src/
######
HAS_JSON=$(shell pkg-config --exists json-c; echo $$?)
ifeq ($(HAS_JSON), 0)
	JSON_INC = $(shell pkg-config --cflags json-c)
	JSON_LIB = $(shell pkg-config --libs json-c)
else
	JSON_HOME=${PWD}/third-party/json-c
	JSON_INC=-I$(JSON_HOME)
	JSON_LIB=$(JSON_HOME)/.libs/libjson-c.a
endif
######
HAS_ZEROMQ=$(shell pkg-config --exists libzmq; echo $$?)
ifeq ($(HAS_ZEROMQ), 0)
	ZEROMQ_INC = $(shell pkg-config --cflags libzmq)
	ZMQ_STATIC=/usr/local/lib/libzmq.a
	ifeq ($(wildcard $(ZMQ_STATIC)),)
		ZEROMQ_LIB = $(shell pkg-config --libs libzmq)
	else
		ZEROMQ_LIB = $(ZMQ_STATIC)
	endif
else
	ZEROMQ_HOME=${PWD}/third-party/zeromq-4.0.5
	ZEROMQ_INC=-I$(ZEROMQ_HOME)/include
	ZEROMQ_LIB=$(ZEROMQ_HOME)/src/.libs/libzmq.a
endif
######
EWH_HOME=${PWD}/third-party/EWAHBoolArray
EWH_INC=$(EWH_HOME)/headers
######
TARGET = ntopng
LIBS = $(NDPI_LIB) $(LIBPCAP) $(LUAJIT_LIB) $(LIBRRDTOOL_LIB) $(ZEROMQ_LIB) $(JSON_LIB) @HIREDIS_LIB@ @SQLITE_LIB@ @LINK_OPTS@ @GEOIP_LIB@ @LDFLAGS@ @PRO_LIBS@ -lm -lpthread
CPPFLAGS = -g @CFLAGS@ @HIREDIS_INC@ $(MONGOOSE_INC) $(JSON_INC) $(NDPI_INC) $(LUAJIT_INC) $(LIBRRDTOOL_INC) $(ZEROMQ_INC) @CPPFLAGS@ -I$(HTTPCLIENT_INC) -I$(EWH_INC) @PRO_INC@ -DDATA_DIR='"$(datadir)"' -I${PWD}/third-party/libgeohash -I${PWD}/third-party/patricia # -D_GLIBCXX_DEBUG
######
# ntopng-1.0_1234.x86_64.rpm
PLATFORM = `uname -p`
REVISION = @GIT_RELEASE@
PACKAGE_VERSION = @PACKAGE_VERSION@
NTOPNG_VERSION = @NTOPNG_VERSION@
RPM_PKG = $(TARGET)-$(NTOPNG_VERSION)-@REVISION@.$(PLATFORM).rpm
RPM_DATA_PKG = $(TARGET)-data-$(NTOPNG_VERSION)-@REVISION@.noarch.rpm
######

ifeq ($(OS),Darwin)
LIBS += -lstdc++.6
endif

LIB_TARGETS =

ifneq ($(HAS_LUAJIT), 0)
LIB_TARGETS += $(LUAJIT_LIB)
 endif

ifneq ($(HAS_ZEROMQ), 0)
LIB_TARGETS += $(ZEROMQ_LIB)
endif

ifneq ($(HAS_LIBRRDTOOL), 0)
LIB_TARGETS += $(LIBRRDTOOL_LIB)
endif

ifneq ($(HAS_JSON), 0)
LIB_TARGETS += $(JSON_LIB)
endif

.PHONY: default all clean docs test

.NOTPARALLEL: default all

default: $(NDPI_LIB_DEP) $(LIB_TARGETS) $(TARGET)

all: default

OBJECTS = $(patsubst src/%.cpp, src/%.o, $(wildcard src/*.cpp)) @PRO_OBJECTS@
HEADERS = $(wildcard include/*.h) @PRO_HEADERS@

%.o: %.c $(HEADERS) Makefile
	$(GPP) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

%.o: %.cpp $(HEADERS) Makefile
	$(GPP) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS) $(LIBRRDTOOL) Makefile
	$(GPP) $(OBJECTS) -Wall $(LIBS) -o $@

$(LUAJIT_LIB):
	cd $(LUAJIT_HOME); @GMAKE@

$(ZEROMQ_LIB):
	cd $(ZEROMQ_HOME); ./configure --without-documentation; @GMAKE@

# --disable-rrd_graph
$(LIBRRDTOOL_LIB):
	cd $(LIBRRDTOOL_HOME); ./configure --disable-libdbi --disable-libwrap --disable-rrdcgi --disable-libtool-lock --disable-nls --disable-rpath --disable-perl --disable-ruby --disable-lua --disable-tcl --disable-python --disable-dependency-tracking --disable-rrd_graph ; cd src; @GMAKE@ librrd_th.la

$(JSON_LIB):
	cd $(JSON_HOME); ./autogen.sh; ./configure; @GMAKE@

clean:
	-rm -f src/*.o src/*~ include/*~ *~ #config.h
	-rm -f $(TARGET)

cert:
	openssl req -new -x509 -sha1 -extensions v3_ca -nodes -days 365 -out cert.pem
	cat privkey.pem cert.pem > httpdocs/ssl/ntopng-cert.pem
	/bin/rm -f privkey.pem cert.pem

veryclean: clean
	-rm -rf nDPI

geoip:
	@if test -d ~/dat_files ; then \
	  cp ~/dat_files/* httpdocs/geoip; gunzip -f httpdocs/geoip/*.dat.gz ; \
	else \
	  cd httpdocs/geoip; \
	  wget -nc http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz; \
	  wget -nc http://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz; \
	  wget -nc http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz; \
	  wget -nc http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNumv6.dat.gz; \
	  gunzip -f *.dat.gz ; \
	fi

# Do NOT build package as root (http://wiki.centos.org/HowTos/SetupRpmBuildEnvironment)
#	mkdir -p $(HOME)/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
#	echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

build-rpm: geoip build-rpm-ntopng build-rpm-ntopng-data


build-rpm-ntopng:
	rpmbuild -bb ./packages/ntopng.spec
	@./packages/rpm-sign.exp $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_PKG)
	@echo ""
	@echo "Package contents:"
	@rpm -qpl $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_PKG)
	@echo "The package is now available in $(HOME)/rpmbuild/RPMS/$(PLATFORM)/$(RPM_PKG)"
	-rm -rf ntopng-@NTOPNG_VERSION@ ntopng_@NTOPNG_VERSION@.tgz
	#

build-rpm-ntopng-data:
	rm -rf ntopng-data-@NTOPNG_VERSION@
	mkdir -p ntopng-data-@NTOPNG_VERSION@/usr/share/ntopng/httpdocs/geoip/
	@if test -d ~/dat_files ; then \
	  cp ~/dat_files/* ntopng-data-@NTOPNG_VERSION@/usr/share/ntopng/httpdocs/geoip/; \
	else \
	  cp ./httpdocs/geoip/* ntopng-data-@NTOPNG_VERSION@/usr/share/ntopng/httpdocs/geoip/ ;\
	fi
	#gunzip ntopng-data-@PACKAGE_VERSION@/usr/local/share/ntopng/httpdocs/geoip/*.gz
	tar cvfz ntopng-data-@NTOPNG_VERSION@.tgz ntopng-data-@NTOPNG_VERSION@
	#
	rm -f $(HOME)/rpmbuild/RPMS/noarch/$(RPM_DATA_PKG)
	cp ntopng-data-@NTOPNG_VERSION@.tgz $(HOME)/rpmbuild/SOURCES/
	cd $(HOME)/rpmbuild/SOURCES;tar xvfz $(HOME)/rpmbuild/SOURCES/ntopng-data-@NTOPNG_VERSION@.tgz
	rpmbuild -bb ./packages/ntopng-data.spec
	@./packages/rpm-sign.exp $(HOME)/rpmbuild/RPMS/noarch/$(RPM_DATA_PKG)
	@echo ""
	@echo "Package contents:"
	@rpm -qpl $(HOME)/rpmbuild/RPMS/noarch/$(RPM_DATA_PKG)
	@echo "The package is now available in $(HOME)/rpmbuild/RPMS/noarch/$(RPM_DATA_PKG)"
	-rm -rf ntopng-data-@NTOPNG_VERSION@ ntopng-data_@NTOPNG_VERSION@.tgz

docs:
	cd doc && doxygen doxygen.conf

dist:
	rm -rf ntopng-@NTOPNG_VERSION@
	mkdir ntopng-@NTOPNG_VERSION@
	cd ntopng-@NTOPNG_VERSION@; git clone https://github.com/ntop/ntopng.git; cd ntopng; git clone https://github.com/ntop/nDPI.git; cd ..; find ntopng -name .git | xargs rm -rf ; mv ntopng ntopng-@NTOPNG_VERSION@; tar cvfz ../ntopng-@NTOPNG_VERSION@.tgz ntopng-@NTOPNG_VERSION@

install: ntopng
	@echo "Make sure you have already run 'make geoip' to also install geoip dat files"
	@echo "While we provide you an install make target, we encourage you"
	@echo "to create a package and install that"
	@echo "rpm - do 'make build-rpm'"
	@echo "deb - do 'cd packages/ubuntu;./configure;make"
	mkdir -p $(INSTALL_DIR)/share/ntopng $(MAN_DIR)/man/man8 $(INSTALL_DIR)/bin
	cp ntopng $(INSTALL_DIR)/bin
	cp ./ntopng.8 $(MAN_DIR)/man/man8
	cp -r ./httpdocs $(INSTALL_DIR)/share/ntopng
	cp -r ./scripts $(INSTALL_DIR)/share/ntopng
	find $(INSTALL_DIR)/share/ntopng -name "*~"   | xargs /bin/rm -f
	find $(INSTALL_DIR)/share/ntopng -name ".git" | xargs /bin/rm -rf

uninstall:
	if test -f $(INSTALL_DIR)/bin/ntopng; then rm $(INSTALL_DIR)/bin/ntopng; fi;
	if test -f $(MAN_DIR)/man/man8/ntopng.8; then rm $(MAN_DIR)/man/man8/ntopng.8; fi;
	if test -d $(INSTALL_DIR)/share/ntopng; then rm -r $(INSTALL_DIR)/share/ntopng; fi;

Makefile: @GIT_INDEX@
	@echo ""
	@echo "Re-running configure as the git release has changed"
	@echo ""
	./configure

cppcheck:
	cppcheck --template='{file}:{line}:{severity}:{message}' --quiet --enable=all --force @HIREDIS_INC@ $(MONGOOSE_INC) $(JSON_INC) $(NDPI_INC) $(LUAJIT_INC) $(LIBRRDTOOL_INC) $(ZEROMQ_INC) -I$(EWH_INC) *.cpp

test: test_version

test_version:
	./ntopng --version

webtest:
	echo "Assuming default HTTP port and default credentials"
	cd /tmp
	rm -rf localhost:3000
	wget --auth-no-challenge -mk --user admin --password admin http://localhost:3000
